package com.briup.bookstore.web.controller;

import com.briup.bookstore.common.constraints.Add;
import com.briup.bookstore.common.constraints.Update;
import com.briup.bookstore.common.response.Result;
import com.briup.bookstore.domain.dto.AddressModifyDTO;
import com.briup.bookstore.domain.vo.AddressVO;
import com.briup.bookstore.service.AddressService;
import jakarta.validation.groups.Default;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@RequiredArgsConstructor
public class AddressController {

    private final AddressService addressService;

    @GetMapping
    public Result<List<AddressVO>> list() {
        List<AddressVO> addressVOS = addressService.listByUserId();
        return Result.success(addressVOS)
                .apply("查询用户地址列表成功");
    }

    /*
        @Valid 注解 和 @Validate 注解都可以用于表示参数校验，
        但是@Valid 注解不能用于表示分组校验，
        而@Validate 注解可以用于表示分组校验，
        可以通过注解内的groups属性对注解进行分组；
        如此校验注解就可以根据条件进行选择在什么时候生效
     */
    @PostMapping
    public Result<?> addAddress(@Validated({Add.class, Default.class}) AddressModifyDTO modifyDTO) {
        addressService.addAddress(modifyDTO);
        return Result.success(null)
                .apply("添加用户地址成功");
    }

    @PutMapping
    public Result<?> updateAddress(
            @RequestBody @Validated({Update.class,Default.class}) AddressModifyDTO modifyDTO) {
        addressService.updateAddress(modifyDTO);
        return Result.success(null)
                .apply("修改用户地址成功");
    }
}
